package com.wang.vedio.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.vondear.rxtool.RxDeviceTool;
import com.wang.vedio.MainActivity2;
import com.wang.vedio.R;
import com.wang.vedio.net.SharedHelper;
import com.wang.vedio.net.StatusBarUtil;
import com.wang.vedio.net.entity.BaseBean;
import com.wang.vedio.net.entity.LoginBean;
import com.wang.vedio.net.rxjava.HttpMethods;
import com.wang.vedio.net.subscribers.ProgressSubscriber;
import com.wang.vedio.net.subscribers.SubscriberOnNextListener;
import com.wang.vedio.player.PasswordActivity;
import com.wang.vedio.utils.StringUtils;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;


public class SplashActivity extends AppCompatActivity {
    private SharedHelper sharedHelper;
    private Context mContext;
    ImageView fmtp;
    private TextView dumiao;

    private TimeCount time;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setDarkMode(this);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        setContentView(R.layout.activity_splash);
        mContext = getApplicationContext();
        time = new TimeCount(5000, 1000);
        fmtp = findViewById(R.id.fmtp);
        dumiao = findViewById(R.id.dumiao);
    }

    private Handler handlerYzm = new Handler();
    private String yqmCode;

    @Override
    protected void onResume() {
        super.onResume();
        handlerYzm.postDelayed(new Runnable() {
            @Override
            public void run() {
                String info = StringUtils.getClipboardContentTest(mContext);
                if (!StringUtils.isEmpty(info) && info.startsWith("PLAYER_")) {
                    yqmCode = info.replace("PLAYER_", "");
                }
                loginApp();
            }
        }, 500);
    }

    class TimeCount extends CountDownTimer {

        TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            dumiao.setText((millisUntilFinished / 1000) + " S");
        }

        @Override
        public void onFinish() {
            dumiao.setVisibility(View.GONE);
            if (resultData.getType() == 0) {
                //第一次注册
                Intent intent = new Intent(SplashActivity.this, PasswordActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(intent);
                return;
            }

            if (StringUtils.isEmpty(resultData.getPassword())) {
                //没有密码，直接进入
                Intent intent = new Intent(SplashActivity.this, MainActivity2.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(intent);
                finish();
                return;
            }
            Intent intent = new Intent(SplashActivity.this, PasswordActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            Bundle bundle = new Bundle();
            bundle.putInt("type", 1);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    }


    private LoginBean resultData;
    private void loginApp() {
        SubscriberOnNextListener<BaseBean<LoginBean>> onNextListener = baseBean -> {
            resultData = baseBean.getData();
            SharedHelper.saveId(mContext, resultData.getId());
            SharedHelper.saveKeyValueForString(mContext, "ncName", resultData.getNcName());
            SharedHelper.saveKeyValueForString(mContext, "inviteNumber", resultData.getInviteNumber());
            String fmImg = resultData.getAddUserName();
            RequestOptions options = new RequestOptions()
                    .placeholder(-1)//图片加载出来前，显示的图片
                    .fallback(-1) //url为空的时候,显示的图片
                    .error(-1);//图片加载失败后，显示的图片
            Glide.with(mContext).load(HttpMethods.BASE_URL + fmImg).apply(options).into(fmtp);
            if (resultData.getType() != 0 && !StringUtils.isEmpty(resultData.getPassword())) {
                SharedHelper.saveKeyValueForString(mContext, "password", resultData.getPassword());
            }
            time.start();
            dumiao.setVisibility(View.VISIBLE);
        };

        Map<String, Object> params = new HashMap<>();//登录上传参数
        params.put("deviceId", RxDeviceTool.getAndroidId(mContext));
        params.put("memberId", SharedHelper.readId(this));
        params.put("inviteMemberId", yqmCode);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), new Gson().toJson(params));
        HttpMethods.getInstance().loginApp(new ProgressSubscriber<>(onNextListener, mContext, false), requestBody);
    }
}
